package cn.com.customer.manager.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import cn.com.customer.manager.Constants
import cn.com.customer.manager.R
import cn.com.customer.manager.application.App
import cn.com.customer.manager.manager.LoginManager
import cn.com.customer.manager.ui.main.MainActivity
import com.xuexiang.xui.XUI
import com.xuexiang.xui.utils.StatusBarUtils
import com.xuexiang.xui.widget.actionbar.TitleBar
import com.xuexiang.xui.widget.actionbar.TitleUtils


/**
 * <p>
 * 描述：所有Activity的基类
 *
 * @author lenovo
 * @version 1.0
 * @date 2018/8/15 15:01
 */
abstract class BaseActivity : AppCompatActivity(), View.OnClickListener {


    /**
     * 当前Activity的实例
     */
    protected lateinit var mActivity: Activity

    /**
     * 当前上下文实例
     */
    protected lateinit var mContext: Context
    private lateinit var titleBar: TitleBar

    var inputMethodManager: InputMethodManager? = null


    var mLoginManager: LoginManager? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        initAppTheme()
        super.onCreate(savedInstanceState)

        StatusBarUtils.setStatusBarLightMode(this)
        when (App.APP_STATUS) {
            App.APP_STATUS_KILLED -> protectApp()
            App.APP_STATUS_NORMAL -> {
                mActivity = this
                mContext = this

                HiActivityStack.getInstance().addActivity(this)
                mLoginManager = LoginManager.getInstance(mContext)
                inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?

                beforeSetContentView()
                setContentView(getLayoutId())

                initView(savedInstanceState)
            }
        }
    }

    /**
     * 初始化ToolBar
     */

    protected fun initActionBar(name: String, isShowLeft: Boolean = true) {

        titleBar= TitleUtils.initTitleBar(mActivity, R.id.titlebar, name)
        if(isShowLeft){
            titleBar.setLeftImageResource(R.mipmap.icon_back_black).setLeftClickListener {
                onTitleBack()
            }
        }
    }

    /**
     * 初始化应用的主题
     */
    @SuppressLint("ResourceAsColor")
    protected open fun initAppTheme() {
        XUI.initTheme(this)
        StatusBarUtils.initStatusBarStyle(this,true,R.color.white)
    }
    open fun getTitleBar(): TitleBar {
        return titleBar;
    }

    open fun protectApp() {
        var intent = Intent(this, MainActivity::class.java)
        intent.putExtra(Constants.KEY_HOME_ACTION, App.APP_STATUS)
        startActivity(intent);
    }

    /**
     * 点击返回按钮
     */
    open fun onTitleBack() {
        hideSoftKeyboard()
        popActivity()
    }

    override fun onBackPressed() {
        onTitleBack()
    }

    /**
     * 设置头部标题
     */
    fun setToolBarTitle(mToolbarTitle: TextView?, title: CharSequence) {
        if (mToolbarTitle != null) {
            mToolbarTitle.text = title
        }
    }

    /**
     * 在setContentView前做的操作
     */
    open fun beforeSetContentView() {}

    /**
     * 获取layout资源
     */
    protected abstract fun getLayoutId(): Int

    /**
     * 初始化控件
     */
    protected abstract fun initView(savedInstanceState: Bundle?)


    override fun onClick(v: View?) {
    }

    override fun onDestroy() {
        super.onDestroy()

        hideSoftKeyboard()

        HiActivityStack.getInstance().finishActivity(this)
    }

    /**
     * 隐藏键盘
     */
    fun hideSoftKeyboard() {
        if (window.attributes.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (currentFocus != null)
                inputMethodManager?.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
        }
    }


    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale != 1f)
        //非默认值
            resources
        super.onConfigurationChanged(newConfig)
    }

    /**
     * 关闭Activity
     */
    private fun popActivity() {
        this.finish()
        System.gc()
    }

    /**
     * 启动Activity
     */
    fun pushActivity(intent: Intent) {
        startActivity(intent)
    }

    /**
     * 启动Activity
     */
    fun pushActivity(i: Intent, code: Int) {
        startActivityForResult(i, code)
    }

    /**
     * 启动Activity
     */
    protected fun startActivity(clazz: Class<*>) {
        startActivity(Intent(mContext, clazz))
    }


    /**
     * 设置 app 字体不随系统字体设置改变
     */
    override fun getResources(): Resources {
        val res = super.getResources()
        if (res != null) {
            var config = res.configuration
            if (config != null && config.fontScale != 1.0f) {
                config.fontScale = 1.0f
                res.updateConfiguration(config, res.getDisplayMetrics())
            }
        }
        return res
    }
}